/* client */

.LC0:
	.string "127.0.0.1"

.LC1:
	.string "please input:"

.LC2:
	.string "end"

.LC3:
	.string "%s\n"


.globl main

main:
	pushq %rbp
	movq %rsp,%rbp
	subq $0x1000,%rsp

	movl $0,-0x1000(%rbp) /* sockfd */
	movl $0,-0xffc(%rbp) /* ser */
	movl $0,-0xfec(%rbp) /* cli */
	movl $0,-0xfdc(%rbp) /* res */
	movl $0,-0xfd8(%rbp) /* buff */
	movl $0,-0xf58(%rbp) /* recvbuff */

	movl $0,%edx
	movl $1,%esi 
	movl $2,%edi 
	movl $0,%eax 
	call socket 
	movl %eax,-0x1000(%rbp) /* sockfd */

	movl $16,%edx
	movl $0,%esi 
	leaq -0xffc(%rbp),%rdi /* ser */
	movl $0,%eax 
	call memset 

	leaq -0xffc(%rbp),%rdi /* ser */
	movw $2,0(%rdi) /* ser.sin_family */
	
	movl $6000,%edi 
	movl $0,%eax
	call htons 

	leaq -0xffc(%rbp),%rdi /* ser */
	movw %ax,2(%rdi) /* ser.sin_port */

	leaq .LC0(%rip),%rdi
	movl $0,%eax
	call inet_addr
	leaq -0xffc(%rbp),%rdi /* ser */
	movl %eax,4(%rdi) /* ser.sin_addr.s_addr */

	movl $16,%edx 
	leaq -0xffc(%rbp),%rsi /* ser */
	movl -0x1000(%rbp),%edi /* sockfd */
	movl $0,%eax 
	call connect 
	movl %eax,-0xfdc(%rbp) /* res */

	jmp .Lmain_400

.Lmain_500:
	leaq .LC1(%rip),%rdi
	movl $0,%eax 
	call printf 

	movq stdout(%rip),%rdi
	movl $0,%eax
	call fflush 

	movl $128,%edx
	movl $0,%esi 
	leaq -0xfd8(%rbp),%rdi /* buff */
	movl $0,%eax
	call memset 

	movq stdin(%rip),%rdx
	movl $128,%esi 
	leaq -0xfd8(%rbp),%rdi /* buff */
	movl $0,%eax 
	call fgets 

	leaq -0xfd8(%rbp),%rdi /* buff */
	movl $0,%eax 
	call strlen 
	subl $1,%eax
	movslq %eax,%rax
	leaq -0xfd8(%rbp),%rdi /* buff */
	movb $0,0(%rdi,%rax,1) /* buff[strlen(buff)-1] */

	leaq .LC2(%rip),%rsi /* end */
	leaq -0xfd8(%rbp),%rdi /* buff */
	movl $0,%eax
	call strcmp
	cmpl $0,%eax
	jne .Lmain_450

	jmp .Lmain_300 /* break */

.Lmain_450:
	movl $0,%ecx
	leaq -0xfd8(%rbp),%rdi /* buff */
	movl $0,%eax 
	call strlen 
	movl %eax,%edx
	leaq -0xfd8(%rbp),%rsi /* buff */
	movl -0x1000(%rbp),%edi /* sockfd */
	movl $0,%eax
	call send

	movl $128,%edx
	movl $0,%esi 
	leaq -0xf58(%rbp),%rdi /* recvbuff */
	movl $0,%eax
	call memset 

	movl $0,%ecx
	movl $127,%edx
	leaq -0xf58(%rbp),%rsi /* recvbuff */
	movl -0x1000(%rbp),%edi /* sockfd */
	movl $0,%eax 
	call recv 

	leaq -0xf58(%rbp),%rsi /* recvbuff */
	leaq .LC3(%rip),%rdi
	movl $0,%eax
	call printf

.Lmain_400:
	jmp .Lmain_500

.Lmain_300:
	movl -0x1000(%rbp),%edi /* sockfd */
	movl $0,%eax 
	call close 

	movl $0,%eax
	leave
	ret
